package ua.com.alus.resvac.crm.stripes.action.account;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.apache.log4j.Logger;
import ua.com.alus.resvac.crm.domen.entities.ResumeAccountDetails;

import java.util.UUID;

/**
 * Create new resumeAccount action
 * <p/>
 * Created by Usatov Alexey
 */
@UrlBinding("/account/RegisterResumeAccount.action")
public class RegisterResumeAccountAction extends RegisterAccount {

    private static final Logger LOGGER = Logger.getLogger(RegisterResumeAccountAction.class);

    private ResumeAccountDetails resumeAccountDetails;

    @DefaultHandler
    @DontValidate
    public Resolution gotoRegisterPage() {
        account = accountService.getAccount(accountService.getUserName());
        if (account != null) {
            resumeAccountDetails = accountService.getResumeAccountDetails(account.getUuid());
        }
        return new ForwardResolution("/public/registeracc.jsp");
    }

    public ResumeAccountDetails getResumeAccountDetails() {
        return resumeAccountDetails;
    }

    @ValidateNestedProperties({
            @Validate(field = "firstName", required = true, minlength = 3, mask = "([A-z]||[А-я])*$"),
            @Validate(field = "lastName", required = true, minlength = 3, mask = "([A-z]||[А-я])*$")
    })
    public void setResumeAccountDetails(ResumeAccountDetails resumeAccountDetails) {
        this.resumeAccountDetails = resumeAccountDetails;
    }

    @Override
    public void saveAccount() {
        if (resumeAccountDetails.getUuid() == null) {
            resumeAccountDetails.setUuid(UUID.randomUUID().toString());
        }
        resumeAccountDetails.setAccountUuid(account.getUuid());
        accountService.saveResumeAccount(account, resumeAccountDetails);
    }


}
